<?php
/**
 * Created by YuanXi.
 * User: YuanXi
 * Date: 2021/6/12 0012
 * Time: 1:45
 */

namespace App\Transformers;


use App\Models\Comment;
use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract
{
    //额外包含的
    protected $availableIncludes = ['goods','user'];
    public function transform(Comment $comment)
    {
        $pics_url = [];
        if(is_array($comment->pics)){
            foreach ($comment->pics as $pic) {
                array_push($pics_url, oss_url($pic));
            }
        }
        return [
            'id' => $comment->id,
            'user_id' => $comment->user_id,
            'goods_id' => $comment->goods_id,
            'rate' => $comment->rate,
            'content' => $comment->content,
            'reply' => $comment->reply,
            'pics' => $comment->pics,
            'pics_url' => $pics_url,
            'created_at' => $comment->created_at,
            'updated_at' => $comment->updated_at,
        ];
    }

    //额外的商品数据
    public function includeGoods(Comment $comment)
    {
        return $this->item($comment->goods,new GoodTransformer());
    }

    //额外的用户数据
    public function includeUser(Comment $comment)
    {
        return $this->item($comment->user,new UserTransformer());
    }
}
